<?php

class Client_Table_Articles extends Apk_Db_Table
{
	protected $_name      = 'articles';
    protected $_primary   = 'id';
    protected $_ERROR = '';
    protected $_lang;

    public function __construct($lang = 'ru')
    {
    	$this->_lang = $lang;
    	parent::__construct();
    }

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetLangID()
    {
			switch($this->_lang)
			{
				case 'ru': return 1;
				case 'ua': return 2;
				case 'en': return 3;
				default: return 1;
			}
    }

    public function GetDataList($page=1, $params=array())
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)
    		  ->joinLeft('categories', 'categories.id = articles.id_cat', array('cat_idt' => 'idt', 'url', 'cat_name' => 'menu_name_'.$this->_lang))
    		  ->where($this->_name.'.is_active = 1')
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.id_lang = ?', $this->GetLangID())
    		  ->where($this->_name.'.is_removed = 0')
    		  ->order($this->_name.'.public_date desc');

    	// **  Params  **
    	if(count($params) > 0)
    	foreach ($params as $k => $v)
    	switch($k)
    	{
    		case 'cats':
    			$sel->where('id_cat in (?)', $v);
    			break;
    		default:
    			$sel->where($this->_name.'.'.$k.' = ?', $v);
    	}

    	//echo $sel; die();

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => 10));
    }

    public function GetLast($params = null, $limit = 2)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'idt', 'title', 'desc', 'public_date', 'path', 'file_name'))
    				->joinLeft('categories', 'categories.id = articles.id_cat', array('cat_idt' => 'idt', 'url', 'cat_name' => 'menu_name_'.$this->_lang))
    				->where($this->_name.'.is_active')
    				->where($this->_name.'.is_removed = 0')
    				->where($this->_name.'.is_ghost = 0')
    				->where($this->_name.'.id_lang = ?', $this->GetLangID())
    				->order($this->_name.'.public_date desc')
    				->limit(3);

        	// **  Params  **
    	if(count($params) > 0)
    	foreach ($params as $k => $v)
    	switch($k)
    	{
    		case 'cats':
    			$sel->where($this->_name.'.id_cat in (?)', $v);
    			break;
    		case 'except':
    			$sel->where($this->_name.'.id != ?', $v);
    			break;
    		default:
    			$sel->where($this->_name.'.'.$k.' = ?', $v);
    	}



    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }

    public function GetTop($limit = 10)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'title', 'desc', 'public_date', 'path', 'file_name', 'idt'))
    				->join('categories', 'categories.id = articles.id_cat', array('cat_idt' => 'idt', 'cat_name' => 'menu_name_ru', 'url'))
    				->where($this->_name.'.is_active')
    				->where($this->_name.'.is_removed = 0')
    				->where($this->_name.'.is_ghost = 0')
    				->where($this->_name.'.is_top = 1')
    				->where($this->_name.'.id_lang = ?', $this->GetLangID())
    				->order($this->_name.'.public_date desc')
    				->limit($limit);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }


    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
		    		->where($this->_name.'.is_active')
		    		->where($this->_name.'.is_removed = 0')
		    		->where($this->_name.'.is_ghost = 0');


		if(intval($id) > 0 && strlen(intval($id)) == strlen($id)) $sel->where($this->_name.'.id = ?', $id);
		else $sel->where($this->_name.'.idt = ?', $id);


		try {
	    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
		}
		catch (Exception $e)
		{
			return array();
		}

    	//$data['tags'] = $this->GetTags($id);
    	return $data;
    }


    public function GetTags($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('tags_to_articles')
    		  ->join('tags', 'tags.id = tags_to_articles.id_tag', array('id', 'name'))
    		  ->where('tags.is_active')
    		  ->where('tags_to_articles.id_articles = ?', $id);

    	return $this->getList($sel)->toArray();
    }

	public function GetNextBack($id = null, $params=array())
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title', 'idt'))
    		  ->joinLeft('categories', 'categories.id = articles.id_cat', array('cat' => 'menu_name_'.$this->_lang, 'cat_idt' => 'idt'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->where($this->_name.'.is_active')
    		  ->where($this->_name.'.id_lang = ?', $this->GetLangID())
    		  ->order(array($this->_name.'.public_date desc'));

    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}

    	try{
    		$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    	}catch(Exception $e)
    	{
    		echo $e->__toString();
    		die();
    	}

    	$ret = array();
    	$ret['count'] = count($data);
    	if($ret['count'] > 0)
    	foreach ($data as $k => $v)
    	if($v['id'] == $id)
    	{
    		$ret['pos'] = $k+1;
    		if($k != $ret['count']-1) $ret['next'] = $data[$k+1];
    		if($k != 0) $ret['back'] = $data[$k-1];
    	}

    	return $ret;
    }





    //**********************************************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {
    	$data['public_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;
    	$data['is_active'] = 1;

        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id = 0)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	//if(!isset($data['is_active'])) $data['is_active'] = 0;
    	//if(!isset($data['is_commented'])) $data['is_commented'] = 0;
    	//if(!isset($data['is_rated'])) $data['is_rated'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id;
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}




    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

    public function topDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            case 'title':
            	$result[$key] = mb_substr($value, 0, 33).'...';

            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>